package com.yuhong.reggie.mapper;

import com.yuhong.reggie.dto.DishDto;
import com.yuhong.reggie.entity.Dish;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface DishMapper {
    //查询该类别的菜总数量
    @Select("select count(*) from dish where category_id=#{cid} and is_deleted=0")
    Integer countByCid(Long cid);

    //插入菜品数据

    @Insert("insert into dish values(null,#{name},#{categoryId},#{price},#{code},#{image},#{description}," +
            "#{status},#{sort},#{createTime},#{updateTime},#{createUser},#{updateUser},0)")
    //默认情况:数据库表中这条记录有主键,但是dishDto的属性id的为null
    //目标:
    @Options(useGeneratedKeys = true,keyColumn = "id",keyProperty = "id")
    Integer Insert(DishDto dishDto);
    //分页查询(连表查)
    List<DishDto> findAll(@Param("name") String name);

    //根据菜品id查询
    DishDto findById(Long id);

    //根据类别id查询菜品的方法
    @Select("select * from dish where category_id=#{categoryId} and is_deleted=0 order by sort asc")
    List<Dish> findByCategoryId(Long categoryId);
    //根据类别id和状态查询菜品列表
    @Select("select * from dish " +
            "where category_id=#{categoryId} and is_deleted=0 and status=#{status} " +
            "order by sort asc")
    List<Dish> findByCategoryIdAndStatus(@Param("categoryId") Long categoryId, @Param("status") Integer status);
}
